<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/20 15:38
 * Desc: 订单退款
 */
class Service_Api_Refund extends Contract_Api_Refund
{

    public function check_refund($mid, $order_id): array
    {
        $data = Model_Api_V2_Refund::check_refund_info($order_id, $mid);
        if (!$data['status']) {
            throw new Exception($data['msg']);
        }
        $refund_data             = $data['refundData'];
        $refund_data['order_id'] = $order_id;
        return $refund_data;
    }

    public function get_info($mid, $order_id): array
    {
        $order = Model_Member_Order::get_order_detail($order_id, $mid);
        if (empty($order)) {
            throw new Exception('查找订单信息失败');
        }

        // 退款信息
        $online_transaction_no = $order['online_transaction_no'];
        $info                  = Pay_Online_Refund::get_refund_info($order);
        if (!isset($info['refund'])) {
            throw new Exception('没有退款信息');
        }



        $refund                 = $info['refund'];
        $refund['typeid']       = $order['typeid'];
        $refund['refund_type']  = $online_transaction_no ? 1 : 0;//退款方式
        $refund['addtime']      = date("Y-m-d H:i", $info['refund']['addtime']);
        $refund['modtime']      = $info['refund']['modtime'] ? date("Y-m-d H:i", $info['refund']['modtime']) : '';
        $refund['refund_proof'] = $info['refund']['refund_proof'] ? $this->img_url($info['refund']['refund_proof']) : '';

        $refund['refund_support']= $info['refund']['refund_support']==1?"仅退款":"退货退款";

        //退款手续费
        $refund_cost = DB::select('refund_cost')->from('member_order_compute')
            ->where('order_id','=', $order_id)
            ->execute()
            ->get('refund_cost');
        if($refund_cost){
            $refund['refund_cost'] = $refund_cost;
            //$refund['refund_fee'] = bcsub($refund['refund_fee'],$refund_cost);
        }
        $refund['refund_fee'] = Num::format($refund['refund_fee'],2);

        unset($refund['memberid']);
        return $refund;
    }

    public function refund_apply($mid, $params): array
    {
        /**
         * @var int $order_id
         * @var $refund_reason
         * @var $platform
         * @var $alipay_account
         * @var $cardholder
         * @var $cardnum
         * @var $bank
         * @var $refund_support
         */
        extract($params);

        $order = Model_Member_Order::get_order_detail($order_id, $mid);
        if (!$order) {
            throw new Exception('订单不存在');
        }

        $refund['ordersn']        = $order['ordersn'];//订单号
        $refund['refund_reason']  = !empty($refund_reason) ? $refund_reason : '';
        $refund['platform']       = !empty($platform) ? $platform : '';
        $refund['alipay_account'] = !empty($alipay_account) ? $alipay_account : '';
        $refund['cardholder']     = !empty($cardholder) ? $cardholder : '';
        $refund['cardnum']        = !empty($cardnum) ? $cardnum : '';
        $refund['bank']           = !empty($bank) ? $bank : '';
        $refund['refund_support']     = $refund_support;

        // 退款处理
        $model_name = Model_Api_V2_Pub::get_model_class_name($typeid);
        $res = Pay_Online_Refund::apply_order_refund($refund, $mid, $model_name);
        if (isset($res['status']) && $res['status'] == 1) {
            return array();
        } else {
            $msg = $refund['message'] ?? '退款申请失败';
            throw new Exception($msg);
        }
    }

    public function cancel_refund_apply($mid, $order_id): string
    {
        $order = Model_Member_Order::get_order_detail($order_id, $mid);
        if (!$order) {
            throw new Exception('订单不存在');
        }

        $order_sn = $order['ordersn'];
        $model_name = Model_Api_V2_Pub::get_model_class_name($order['typeid']);
        $result  = Pay_Online_Refund::order_refund_back($order_sn, $mid, $model_name);

        if(!$result['bool']){
            throw new Exception($result['message']);
        }

        return $result['message'];
    }
}